<template>
  <div id="register" v-title data-title="注册 - 动物保护协会">
    <video
      preload="auto"
      class="me-video-player"
      autoplay="autoplay"
      loop="loop"
    >
      <source src="../../static/vedio/sea.mp4" type="video/mp4" />
    </video>

    <div class="me-login-box me-login-box-radius">
      <h1>动物保护协会 注册</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="userName">
          <el-input placeholder="用户名" v-model="userForm.userName"></el-input>
        </el-form-item>

        <el-form-item prop="email">
          <el-input placeholder="邮箱" v-model="userForm.email"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            v-model="userForm.password"
          ></el-input>
        </el-form-item>

        <el-form-item prop="checkPass">
          <el-input
            type="password"
            v-model="userForm.checkPass"
            placeholder="确认密码"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="checkCode" ref="codeValue">
          <el-input
            v-model="userForm.checkCode"
            autocomplete="off"
            placeholder="验证码"
            style="width: 60%"
          ></el-input>
          <el-button type="primary" @click="getCode">获取验证码</el-button>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="register('userForm')"
            >注册</el-button
          >
        </el-form-item>
      </el-form>

      <div class="me-login-design">
        <p>
          <strong>
            <router-link to="/" class="me-login-design-color">首页</router-link>
          </strong>
          <strong>
            <router-link to="/login" class="me-login-design-color"
              >登录</router-link
            >
          </strong>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import { register } from "@/api/login";

export default {
  name: "Register",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.userForm.checkPass !== "") {
          this.$refs.userForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.userForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userForm: {
        userName: "",
        email: "",
        password: "",
        checkCode: "",
        checkPass: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 20, message: "不能大于20个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { max: 30, message: "不能大于30个字符", trigger: "blur" },
        ],
        password: [
          { validator: validatePass, trigger: "blur", required: true },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        checkPass: [
          { validator: validatePass2, trigger: "blur", required: true },
        ],
        checkCode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    register(formName) {
      // let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {

          const _this = this;
          this.$axios.post("/user/addUser", this.userForm).then((res) => {
            console.log(res);
            _this.$alert("注册成功", "提示", {
              confirmButtonText: "去登录",
              callback: (action) => {
                // 跳转页面
                _this.$router.push("/login");
              },
            });
          });


        } else {
          return false;
        }
      });
    },

    /*获取验证码*/
    getCode() {
      this.$axios.post("/makeCheckCode", this.userForm).then((res) => {
        console.log(res);
        Element.Message({
          showClose: true,
          message: res.data.data,
          type: "success",
        });
        if (res.data.code == 200) {
          let times = 60;
          var timer = setInterval(() => {
            times--;
            if (times === 0) {
              times = "获取验证码";
              this.tip = times;
              clearInterval(timer);
              this.events = false;
            } else {
              this.tip = times + "s";
              this.events = true;
            }
          }, 1000);
        }
      });
    },
  },
};
</script>

<style scoped>
#login {
  min-width: 100%;
  min-height: 100%;
}

.me-video-player {
  background-color: transparent;
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  position: absolute;
  left: 0;
  z-index: 0;
  top: 0;
}

.me-login-box {
  position: absolute;
  width: 300px;
  /* height: 320px; */
  background-color: white;
  margin-top: 150px;
  margin-left: -180px;
  left: 50%;
  padding: 30px;
}

.me-login-box-radius {
  border-radius: 10px;
  box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
}

.me-login-box h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
  vertical-align: middle;
}

.me-login-design {
  text-align: center;
  font-family: "Open Sans", sans-serif;
  font-size: 18px;
}

.me-login-design-color {
  color: #5fb878 !important;
}

.me-login-button {
  text-align: center;
}

.me-login-button button {
  width: 100%;
}
</style>
